<?php
$caption = $row['name'] . ' at ' . $row['address'] . ', ' . $row['postcode'] . ' ' . $row['city'];
$restaurants_lbl = BUtils::T('restaurants_lbl');
$this->pageTitle = $caption . ' | Yamii';

$this->breadcrumbs=array(
	$restaurants_lbl => array('/restaurant'),
	$row['name'],
);

//define array
$aWeekDaysShort = BUtils::WeekDayKeys(7, true);
$aOpenningHours = BUtils::AllHour24();
//set open hours
$hours_list = '';
if (!empty($aHours)) {
	//set open hours display style
	$sWeekBuffer = array();
	foreach ($aHours as $v) {
		$sWeekday = $aWeekDaysShort[$v['week_no']];
		$sHours = $aOpenningHours[$v['open_hour']] . ' - ' . $aOpenningHours[$v['close_hour']];
		$sWeekBuffer[$sHours][] = $v['week_no'];//add the current week day
	}
	$i = 0;
	foreach ($sWeekBuffer as $key => $v) {
		$size = count($v);
		if ($size > 1) $sWeekday = $aWeekDaysShort[$v[0]] . '-' . $aWeekDaysShort[$v[$size-1]];
		else $sWeekday = $aWeekDaysShort[$v[0]];
		$icon = ($i++ === 0) ? '<i class="gicon-clock"></i>' : '<i class="gicon-blank"></i>';
		$hours_list .= $icon . ' <span class="weekday">'.$sWeekday.'</span> <span class="hours">'.$key.'</span><br />';
	}
}

$full_address = $row['address'] . ', ' . $row['postcode'] . ' ' . $row['city'];
$url_logo = BUtils::GetBusinessPictureUrl($row['url_logo']);
$brief = BUtils::GetText($row['brief']);

//get the latLng
if (empty($row['latitude']) or empty($row['longitude'])) {
	$latitude = 'null';
	$longitude = 'null';
	$address = "'{$full_address}'";
} else {
	$latitude = $row['latitude'];
	$longitude = $row['longitude'];
	$address = 'null';
}
?>
<div class="row">
	<div class="span8">
		<div class="box_white relative">
			<div class="panel clearfix place-info">
				<div class="logo">
					<a class="pic" style="display:block; margin-bottom:8px;"><img src="<?php echo $url_logo; ?>" alt="" /></a>
					<?php
					if (Yii::app()->user->isGuest) {
						$btn_name = BUtils::T('follow_btn');
						$link = Yii::app()->createUrl('/accounts/signup');
						$class = 'login-popup';
					} else {
						//start to make following button
						if (isset($status['follow']) and $status['follow'] == true) {
							$btn_name = BUtils::T('follow_cancel_btn');
							$link = Yii::app()->createUrl('/place/follow', array('id'=>$row['id']));
							$class = 'follow-place active';
						} else {
							$btn_name = BUtils::T('follow_btn');
							$link = Yii::app()->createUrl('/place/follow', array('id'=>$row['id']));
							$class = 'follow-place';
						}
					}
					$this->widget('bootstrap.widgets.TbButton', array(
						'buttonType'=>'link', 'type'=>'info',
						'label'=>$btn_name, 'url'=>$link, 'loadingText'=>'waiting...',
						'htmlOptions'=>array('class'=>$class, 'style'=>'width:78px;'),
					));
					?>
				</div>
				<h1 class="name"><?php echo $row['name']; ?></h1>
				<div class="address">
					<i class="gicon-google-maps"></i>
					<span class="street"><?php echo $row['address']; ?></span>,
					<span class="city"><?php echo $row['postcode'] . ' ' . $row['city']; ?></span>
				</div>
<?php
//handle the phone number
if (!empty($row['phone'])) {
	echo <<<HTML
<div class="phone">
	<i class="gicon-phone"></i>
	{$row['phone']}
</div>
HTML;
}
//handle the website link
if (!empty($row['url_site']) and $row['url_site'] != 'http://') {
	echo <<<HTML
<div class="site-url tag-more">
	<i class="gicon-link"></i>
	<a href="{$row['url_site']}" target="_blank">{$row['url_site']}</a>
</div>
HTML;
}
//handle the business hours
if (!empty($hours_list)) {
	echo '<div class="open-hours tag-more">'.$hours_list.'</div>';
}
?>
			</div>
		</div>
		
		<div class="row clearfix">
<?php
if (empty($gallery_rows)) {
	$classSpan = "span8";
} else {
	$classSpan = 'span5';
	$pictureLinks = '';
	foreach ($gallery_rows as $gal) {
		$link = BUtils::GetBusinessPictureUrl($gal['url_picture']);
		$thumb200Link = str_replace('thumb_', 'thumb_200h_', $link);
		$fileLink = str_replace('thumb_', '', $link);
		$pictureLinks .= '<a class="thumb" href="'.$fileLink.'" title="'.$gal['title'].'"><img src="'.$thumb200Link.'" alt="" /></a> ';
	}
	echo <<<HTML
<div class="span3">
	<div class="box_white fill-right">
		<div class="media fixed-height gallery-space">
			{$pictureLinks}
		</div>
	</div>
</div>
HTML;
}
?>
			<div class="<?php echo $classSpan; ?>">
				<div class="box_white">
					<div id="venueMap" class="media fixed-height" data-longitude="<?php echo $longitude; ?>" data-latitude="<?php echo $latitude; ?>"></div>
				</div>
			</div>
		</div>
		<div class="row clearfix">
<?php if (!empty($otherDishes)) { ?>
			<div class="span8">
				<div class="box_white">
					<div class="title-bar">
						<h4 class="green"><?php echo BUtils::T('dish_featured_lbl'); ?></h4>
					</div>
				</div>
				<div class="waterfall fix-height clearfix">
<?php
foreach ($otherDishes as $i => $d) {
	$number = $i + 1;
	$urlThumb = BUtils::GetBusinessPictureUrl($d['url_picture']);
	$urlPicture = str_replace('thumb_', '', $urlThumb);
	$price = Yii::app()->locale->numberFormatter->formatCurrency($d['price'], 'EUR');
	$name = CHtml::encode($d['name']);
	$link = Yii::app()->createUrl('/dish/view', array('id'=>$d['id']));
	echo <<<HTML
<div class="item" style="position:relative;" data-id="{$d['id']}">
	<div class="top-info">
		<p class="name">{$name}</p>
		<p class="price">{$price}</p>
	</div>
	<div class="bottom-info">
		<a href="{$urlPicture}" class="pictureview-btn" title="{$name} - {$price}"><img src="{$urlThumb}" class="dish-photo" alt="" /></a>
	</div>
	<div class="bar-5px"></div>
</div>
HTML;
}
?>
				</div>
			</div>
<?php } ?>
		</div>
	</div>

	<!-- right side -->
	<div class="span4">

<?php if (!empty($special_rows)) { ?>
		<div class="box_white">
			<div class="title-bar">
				<h4 class="red"><?php echo BUtils::T('special_lbl'); ?></h4>
			</div>
			<div class="panel panel-with-title">
<?php
foreach ($special_rows as $k => $spc) {
	//valida date
	$selected_valid_date = BUtils::GetDateRangeString($spc['uts_start_date'], $spc['uts_end_date'], 'd.m');
	// valid time
	$selected_hour_range = BUtils::GetHourRangeFormat($spc['start_hour'], $spc['end_hour']);
	// week days
	$selected_week_days = BUtils::HtmlWeekdays($spc['week_days'], $aWeekDaysShort);
	//discount
	$discount_text = null;
	if (!empty($spc['discount'])) {
		$discount_text = '-' . round($spc['discount']) . '%';
	}
	//title
	$title_text = BUtils::T('special_auto_title_lbl', array(
		'{merchant_name}' => $row['name'],
		'{price}' => Yii::app()->locale->numberFormatter->formatCurrency($spc['price'], 'EUR'),
		'{value}' => Yii::app()->locale->numberFormatter->formatCurrency($spc['value'], 'EUR'),
	));
	$link_url = Yii::app()->createUrl('/special/view', array('id'=>$spc['id']));
	$link_button = CHtml::link($title_text, $link_url, array('class'=>'preview-btn'));
	echo <<<HTML
<div class="specialItem clearfix">
	<div class="discount-box">
		<span>{$discount_text}</span>
	</div>
	<div class="detail-box">
		<div class="title">{$link_button}</div>
		<div class="period clearfix">
			<div class="left">{$selected_hour_range}</div>
			<div class="right">{$selected_week_days}</div>
		</div>
	</div>
</div>
HTML;
}
?>
			</div>
		</div>
<?php } ?>

<?php
$tag_labels = null;
if (!empty($row['cuisine_style'])) $tag_labels['cuisine_style'] = $row['cuisine_style'];
if (!empty($row['cuisine_type'])) $tag_labels['cuisine_type'] = $row['cuisine_type'];
if (!empty($row['meal_served'])) $tag_labels['meal_served'] = $row['meal_served'];
if (!empty($row['payment_options'])) $tag_labels['payment_options'] = $row['payment_options'];
if (!empty($row['service_languages'])) $tag_labels['service_languages'] = $row['service_languages'];
if (!empty($row['alcohol_license'])) $tag_labels['alcohol_license'] = $row['alcohol_license'];
if (!empty($row['dining_ambience'])) $tag_labels['dining_ambience'] = $row['dining_ambience'];
if (!empty($row['car_parking'])) $tag_labels['car_parking'] = $row['car_parking'];
if (!empty($row['additional_details'])) $tag_labels['additional_details'] = $row['additional_details'];
if (!empty($tag_labels)) {
	$tags_html = null;
	foreach ($tag_labels as $key => $value) {
		if (!empty($value)) {
			$string = null;
			$array = explode(',', $value);
			foreach ($array as $item) {
				$string .= '<span class="label" style="background-color:#0ae">' . BUtils::T($item) . '</span> ';
			}
			$tags_html .= '<div style="font-weight:bold; margin-top:10px;">' . BUtils::T($key . '_lbl') . ':</div><div style="margin-left:40px;">'.$string.'</div>';
		}
	}
?>
<div class="box_white">
	<div class="title-bar">
		<h4 class="blue"><?php echo BUtils::T('detail_features_lbl'); ?></h4>
	</div>
	<div class="panel panel-with-title">
		<div class="dishItems clearfix">
			<?php echo $tags_html; ?>
		</div>
	</div>
</div>
<?php } ?>

<?php if (!empty($brief)) { ?>
		<div class="box_white">
			<div class="title-bar">
				<h4 class="blue"><?php echo BUtils::T('merchant_brief_lbl'); ?></h4>
			</div>
			<div class="panel panel-with-title">
				<div class="dishItems clearfix">
					<?php echo $brief; ?>
				</div>
			</div>
		</div>
<?php } ?>
	</div>
</div>
<script type="text/javascript">
$(function() {
	//more tags
	$('.more-tags').toggle(function() {
		var logHeight = $('.logo').height();
		$('.place-info').height(logHeight);//fix the height
		$(this).html('less');
		$('.tag-more').show();
	}, function() {
		$('.place-info').attr('style','');//remove the height
		$(this).html('more');
		$('.tag-more').hide();
	})
	
	//popup special box and gallery
	$('.pictureview-btn').colorbox({rel:'dish',transition:'none'});
	$('.preview-btn').colorbox({rel:'coupon',transition:'none'});
	$('.gallery-space a.thumb').colorbox({rel:'thumb',transition:'none'});
	
	//init the map
	var mapCav = $("#venueMap").isMapCanvas('<?php echo Yii::app()->baseUrl; ?>', 1);
	mapCav.pin('mid_<?php echo $row['name']; ?>', {
		style:3,
		latitude: <?php echo $latitude; ?>,
		longitude: <?php echo $longitude; ?>
	});
});
</script>